package symptomsSearch;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.naming.NamingException;

import ctm.javacode.dbBean;

public class AttributeQuery {

	private LinkedList<String> atrribue_value_not_exist = new LinkedList<String>();
	private LinkedList<String> object_not_exist = new LinkedList<String>();

	public List<List<String>> ValueAttributeQuery(
			LinkedList<String> attribute_value) throws NamingException,
			SQLException {

		dbBean db = new dbBean("jdbc/ctmpllab");
		db.Connected();

		List<List<String>> AttributeAndValueList = new ArrayList<List<String>>();

		String value_name;
		String parent_name = "";

		for (int attribute_value_idx = 0; attribute_value_idx < attribute_value
				.size(); attribute_value_idx++) {

			String sqlQuery = "Select * from standard_table A1,standard_table A2 where (A1.name ='"
					+ attribute_value.get(attribute_value_idx)
					+ "' and FIND_IN_SET( A2.id , A1.parent_id))";

			ResultSet rs;
			rs = db.ExcuteQuery(sqlQuery);

			if (!rs.next()) {

				atrribue_value_not_exist.add(attribute_value
						.get(attribute_value_idx));

			} else {
				do {

					List<String> AttributeAndValueData = new ArrayList<String>();
					parent_name = rs.getString("A2.name");
					value_name = rs.getString("A1.name");

					AttributeAndValueData.add(parent_name);
					AttributeAndValueData.add(value_name);
					AttributeAndValueList.add(AttributeAndValueData);

				} while (rs.next());

			}

		}
		db.CloseConn();
		return AttributeAndValueList;

	}

	public List<List<String>> ObjectAttributeQuery(LinkedList<String> object)
			throws NamingException, SQLException {

		dbBean db = new dbBean("jdbc/ctmpllab");
		db.Connected();
		List<List<String>> ObgectAndAttributeList = new ArrayList<List<String>>();

		for (int object_idx = 0; object_idx < object.size(); object_idx++) {

			List<String> ObgectAndAttributeData = new ArrayList<String>();

			String sqlQuery2 = "Select A1.name,A2.name from standard_table A1,standard_table A2 where (A1.name ='"
					+ object.get(object_idx)
					+ "' and FIND_IN_SET( A1.id , A2.parent_id))";

			ResultSet rs2;
			rs2 = db.ExcuteQuery(sqlQuery2);

			if (!rs2.next()) {

				object_not_exist.add(object.get(object_idx));

			}

			else {

				ObgectAndAttributeData.add(object.get(object_idx));

				do {

					ObgectAndAttributeData.add(rs2.getString("A2.name"));

				} while (rs2.next());

				ObgectAndAttributeList.add(ObgectAndAttributeData);

			}

		}
		db.CloseConn();
		return ObgectAndAttributeList;

	}

}
